<script>
    import Vue from 'vue';
    // #ifdef APP-PLUS
    import Update from '@/utils/update.js';
    // #endif
    import {
        mapMutations
    } from 'vuex';
    export default {
        onLaunch(options) {
            // 初始化app
            this.initApp(options);
            this.handleInit();
            // #ifdef APP-PLUS
            this.update = new Update();
            // #endif

            let systemInfo = uni.getSystemInfoSync();
            let menuButtonInfo = {};

            // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
            menuButtonInfo = uni.getMenuButtonBoundingClientRect();
            Vue.prototype.menuButtonInfo = menuButtonInfo;
            // #endif

            Vue.prototype.systemInfo = systemInfo;
            Vue.prototype.statusBarHeight = systemInfo.statusBarHeight;
            // #ifdef APP-PLUS || H5
            Vue.prototype.navbarHeight = 44;
            // #endif
            // #ifdef MP
            Vue.prototype.navbarHeight = systemInfo.platform == 'ios' ? 44 : 48;
            // #endif

            // #ifdef MP-WEIXIN
            this.handleWxMpSilenceLogin(); // 小程序静默登录
            // #endif

            // #ifndef MP-WEIXIN
            this.handleGetUserInfo(); // 获取用户信息
            // #endif
        },
        onShow(options) {
            // #ifdef MP-WEIXIN
            // 检测小程序更新
            this.$checkWxMpUpdate();
            // #endif
            // #ifdef APP-PLUS
            // 检测App更新
            this.update.checkAppUpdate();
            // #endif
        },
        methods: {
            handleInit() {
                this.$api.util.init().then(res => {
                    console.log('init', res)
                    this.$setState('appConfig', res.mp);
                })
            },
            // #ifdef MP-WEIXIN
            // 小程序静默登录
            handleWxMpSilenceLogin() {
                console.log('handleWxMpSilenceLogin token', this.token)
                if (this.token) {
                    this.handleGetUserInfo();
                } else {
                    this.$wxMpSilenceLogin()
                        .then(res => {
                            this.handleGetUserInfo();
                        })
                        .catch(err => {});
                }
            },
            // #endif
            // 获取用户信息
            handleGetUserInfo() {
                this.$request
                    .post('/api/mp/core/member/info', {}, {
                        handleError: false
                    })
                    .then(res => {
                        this.$setState('login', true);
                        this.setUserInfo(res.info);
                    })
                    .catch(err => {
                        // 清除数据
                        this.closeApp();
                        // #ifndef MP-WEIXIN

                        // #endif
                    });
            },
            ...mapMutations(['initApp', 'closeApp', 'setUserInfo'])
        }
    };
</script>

<style lang="scss">
    /*每个页面公共css */
    @import 'styles/layout.scss';
    @import 'styles/common.scss';
    @import 'styles/iconfont.scss';
    @import 'styles/animation.scss';
    @import '@/uni_modules/uview-ui/index.scss';

    ::v-deep .u-form-item {

        .u-input,
        .u-textarea {
            background-color: #fff !important;
            border: none;
        }
    }
</style>